<div class="container">
    <div class="page-header">
        <h1>{if $id}Редактирование параметров привязки{else}Привязка{/if} баннера "{$aBanner.title}"</h1>
    </div>
    <form action="{$cururl}" method="post" class="form-horizontal" enctype="multipart/form-data">
    <div class="row">
        <div class="span6">
        	<fieldset>
                <div class="control-group{if $errors.site_id} error{/if}">
                    <label class="control-label">Сайт</label>
                    <div class="controls">
                        <select name="site_id" id="site_id">
                            <option value="0">-- Выберите --</option>
                            {foreach from=$aSites item=item}
                                <option value="{$item.id}"{if $aData.site_id == $item.id} selected{/if}>{$item.title}</option>
                            {/foreach}
                        </select>
                        <p class="help-block{if !$errors.site_id} invisible{/if}">{$errors.site_id|default:'&nbsp;'}</p>
                    </div>
                </div>
                <div class="control-group{if $errors.place_id} error{/if}">
                    <label class="control-label">Место</label>
                    <div class="controls">
                        <select name="place_id" id="place_id" disabled="disabled">
                            <option value="0">-- Выберите --</option>
                        </select>
                        <p class="help-block{if !$errors.place_id} invisible{/if}">{$errors.place_id|default:'&nbsp;'}</p>
                    </div>
                </div>
                <div class="control-group{if $errors.start_date} error{/if}">
                    <label class="control-label">Дата начала</label>
                    <div class="controls">
                        <input type="text" name="start_date" id="start_date" value="{$aData.start_date}">
                        <p class="help-block{if !$errors.start_date} invisible{/if}">{$errors.start_date|default:'&nbsp;'}</p>
                    </div>
                </div>
                <div class="control-group{if $errors.end_date} error{/if}">
                    <label class="control-label">Дата окончания</label>
                    <div class="controls">
                        <input type="text" name="end_date" id="end_date" value="{$aData.end_date}">
                        <p class="help-block{if !$errors.end_date} invisible{/if}">{$errors.end_date|default:'&nbsp;'}</p>
                    </div>
                </div>
                <div class="control-group{if $errors.host_limit} error{/if}">
                    <label class="control-label">Host Limit</label>
                    <div class="controls">
                        <input type="text" name="host_limit" value="{$aData.host_limit|default:'0'}">
                        <p class="help-block{if !$errors.host_limit} invisible{/if}">{$errors.host_limit|default:'&nbsp;'}</p>
                    </div>
                </div>
                <div class="control-group{if $errors.traffic_limit} error{/if}">
                    <label class="control-label">Traffic Limit</label>
                    <div class="controls">
                        <input type="text" name="traffic_limit" value="{$aData.traffic_limit|default:'0'}">
                        <p class="help-block{if !$errors.traffic_limit} invisible{/if}">{$errors.traffic_limit|default:'&nbsp;'}</p>
                    </div>
                </div>
                <div class="control-group{if $errors.avg_visits} error{/if}">
                    <label class="control-label">Average visits</label>
                    <div class="controls">
                        <input type="text" name="avg_visits" value="{$aData.avg_visits|default:'0'}">
                        <p class="help-block{if !$errors.avg_visits} invisible{/if}">{$errors.avg_visits|default:'&nbsp;'}</p>
                    </div>
                </div>
                <div class="control-group{if $errors.priority} error{/if}">
                    <label class="control-label">Priority</label>
                    <div class="controls">
                        <select name="priority">
                            <option value="empty">-- Выберите --</option>
                            {foreach from=$aPriority key=key item=item}
                            <option value="{$key}"{if $aData.priority == $key} selected="selected"{/if}>{$item}</option>
                            {/foreach}
                        </select>
                        <p class="help-block{if !$errors.priority} invisible{/if}">{$errors.priority|default:'&nbsp;'}</p>
                    </div>
                </div>
        	</fieldset>
        </div>
        <div class="span6">
            <fieldset>
                <div class="control-group{if $errors.pages} error{/if}">
                    <label class="control-label">Страницы</label>
                    <div class="controls">
                        <label class="checkbox">
                            <input type="hidden" name="show_only_on_main" value="0">
                            <input type="checkbox" name="show_only_on_main" id="show_only_on_main" value="1"
                            {if $aData.show_only_on_main}checked="checked"{/if}
                            > Показывать только на главной
                        </label>
                        <textarea name="pages"
                        {if $aData.show_only_on_main}disabled="disabled"{/if}
                        >{$aData.pages}</textarea>
                        <p class="help-block{if !$errors.pages} invisible{/if}">{$errors.pages|default:'&nbsp;'}</p>
                    </div>
                </div>
                <div class="control-group{if $errors.zones} error{/if}">
                    <label class="control-label">Зоны</label>
                    <div class="controls">
                        <textarea name="zones">{$aData.zones}</textarea>
                        <p class="help-block{if !$errors.zones} invisible{/if}">{$errors.zones|default:'&nbsp;'}</p>
                    </div>
                </div>
                <div class="control-group{if $errors.clicks_limit} error{/if}">
                    <label class="control-label">Лимит кликов</label>
                    <div class="controls">
                        <input type="text" name="clicks_limit" value="{$aData.clicks_limit|default:'0'}">
                        <p class="help-block{if !$errors.clicks_limit} invisible{/if}">{$errors.clicks_limit|default:'&nbsp;'}</p>
                    </div>
                </div>
                <div class="control-group{if $errors.views_limit} error{/if}">
                    <label class="control-label">Лимит показов</label>
                    <div class="controls">
                        <input type="text" name="views_limit" value="{$aData.views_limit|default:'0'}">
                        <p class="help-block{if !$errors.views_limit} invisible{/if}">{$errors.views_limit|default:'&nbsp;'}</p>
                    </div>
                </div>
                <div class="control-group{if $errors.daily_clicks_limit} error{/if}">
                    <label class="control-label">Дн. лимит кликов</label>
                    <div class="controls">
                        <input type="text" name="daily_clicks_limit" value="{$aData.daily_clicks_limit|default:'0'}">
                        <p class="help-block{if !$errors.daily_clicks_limit} invisible{/if}">{$errors.daily_clicks_limit|default:'&nbsp;'}</p>
                    </div>
                </div>
                <div class="control-group{if $errors.daily_views_limit} error{/if}">
                    <label class="control-label">Дн. лимит показов</label>
                    <div class="controls">
                        <input type="text" name="daily_views_limit" value="{$aData.daily_views_limit|default:'0'}">
                        <p class="help-block{if !$errors.daily_views_limit} invisible{/if}">{$errors.daily_views_limit|default:'&nbsp;'}</p>
                    </div>
                </div>
            </fieldset>
        </div>
    </div>
    <div class="row">
        <div class="form-actions">
            <button class="btn btn-success" type="submit"><i class="icon-check icon-white"></i> Сохранить</button>
        </div>
    </div>
    </form>
</div>

{literal}
<script type="text/javascript">

var iPlaceId = {/literal}{$aData.place_id|default:'0'}{literal};

$(document).ready(function()
{
    $('#site_id').change(function(){
        $('#place_id option:gt(0)').remove();
        var iSiteId = parseInt($(this).val());
        if (iSiteId)
        {
            $.ajax({
                url: '/manager/link/getSitePlacesList/' + iSiteId,
                dataType: 'json',
                success: function(response){
                    if (response.success){
                        var aPlaces = response.data;
                        for (var i in aPlaces)
                        {
                            $('#place_id').append(
                                '<option value="'+aPlaces[i]['id']+'"'+
                                (aPlaces[i]['id'] == iPlaceId ? 'selected="selected"':'')+'>'+
                                aPlaces[i]['title']+'</option>');
                        }
                        $('#place_id').removeAttr('disabled');
                    }
                }
            });
        } else {
            $('#place_id').attr('disabled', 'disabled');
        }
    });

    $('#show_only_on_main').click(function(){
        if ($(this).attr('checked'))
        {
            $('textarea[name="pages"]').attr('disabled', 'disabled');
        }
        else
        {
            $('textarea[name="pages"]').removeAttr('disabled');
        }
    });

    $('#site_id').change();
});

$('#start_date').datetimepicker({
    onClose: function(dateText, inst) {
        var endDateTextBox = $('#end_date');
        if (endDateTextBox.val() != '') {
            var testStartDate = new Date(dateText);
            var testEndDate = new Date(endDateTextBox.val());
            if (testStartDate > testEndDate)
                endDateTextBox.val(dateText);
        }
        else {
            endDateTextBox.val(dateText);
        }
    },
    onSelect: function (selectedDateTime){
        var start = $(this).datetimepicker('getDate');
        $('#end_date').datetimepicker('option', 'minDate', new Date(start.getTime()));
    }
});
$('#end_date').datetimepicker({
    onClose: function(dateText, inst) {
        var startDateTextBox = $('#start_date');
        if (startDateTextBox.val() != '') {
            var testStartDate = new Date(startDateTextBox.val());
            var testEndDate = new Date(dateText);
            if (testStartDate > testEndDate)
                startDateTextBox.val(dateText);
        }
        else {
            startDateTextBox.val(dateText);
        }
    },
    onSelect: function (selectedDateTime){
        var end = $(this).datetimepicker('getDate');
        $('#start_date').datetimepicker('option', 'maxDate', new Date(end.getTime()));
    }
});

</script>
{/literal}